#!/bin/bash

set -e

echo ""

echo -en "VMail mail delivery path [/home/vmail]: "
read temp; INPUT_MAIL_DIR=${temp:-/home/vmail}

# TODO: Add workaround for missing /etc/mailname
echo -en "VMail server name [`cat /etc/mailname`]: "
read temp; INPUT_HOSTNAME=${temp:-`cat /etc/mailname`}
if [ ! -f /etc/mailname ]
then
	echo $INPUT_HOSTNAME > /etc/mailname
fi

echo -en "VMail MySQL database name to create [configuration]: "
read temp; INPUT_DATABASE=${temp:-configuration}

echo -en "VMail password for MySQL database [foobar123]: "
read temp; INPUT_PASSWORD=${temp:-foobar123}

rm -rf /tmp/vmailconfig
mkdir /tmp/vmailconfig
echo ""

################################################################################

echo "Updating /etc/postfix/sasl/smtpd.conf...";
sed s/__DATABASE__/$INPUT_DATABASE/g < /etc/postfix/sasl/smtpd.conf > /tmp/vmailconfig/smtpd.conf.1
sed s/__PASSWORD__/$INPUT_PASSWORD/g < /tmp/vmailconfig/smtpd.conf.1 > /tmp/vmailconfig/smtpd.conf.2
cp /tmp/vmailconfig/smtpd.conf.2 /etc/postfix/sasl/smtpd.conf
rm -f /tmp/vmailconfig/smtpd.conf*

################################################################################

echo "Updating /etc/postfix/vmail/mysql.domains.cf...";
sed s/__DATABASE__/$INPUT_DATABASE/g < /etc/postfix/vmail/mysql.domains.cf > /tmp/vmailconfig/mysql.domains.cf.1
sed s/__PASSWORD__/$INPUT_PASSWORD/g < /tmp/vmailconfig/mysql.domains.cf.1 > /tmp/vmailconfig/mysql.domains.cf.2
cp /tmp/vmailconfig/mysql.domains.cf.2 /etc/postfix/vmail/mysql.domains.cf
rm -f /tmp/vmailconfig/mysql.domains.cf*

################################################################################

echo "Updating /etc/postfix/vmail/mysql.mailboxes.cf...";
sed s/__DATABASE__/$INPUT_DATABASE/g < /etc/postfix/vmail/mysql.mailboxes.cf > /tmp/vmailconfig/mysql.mailboxes.cf.1
sed s/__PASSWORD__/$INPUT_PASSWORD/g < /tmp/vmailconfig/mysql.mailboxes.cf.1 > /tmp/vmailconfig/mysql.mailboxes.cf.2
cp /tmp/vmailconfig/mysql.mailboxes.cf.2 /etc/postfix/vmail/mysql.mailboxes.cf
rm -f /tmp/vmailconfig/mysql.mailboxes.cf*

################################################################################

echo "Updating /etc/postfix/vmail/mysql.aliases.cf...";
sed s/__DATABASE__/$INPUT_DATABASE/g < /etc/postfix/vmail/mysql.aliases.cf > /tmp/vmailconfig/mysql.aliases.cf.1
sed s/__PASSWORD__/$INPUT_PASSWORD/g < /tmp/vmailconfig/mysql.aliases.cf.1 > /tmp/vmailconfig/mysql.aliases.cf.2
cp /tmp/vmailconfig/mysql.aliases.cf.2 /etc/postfix/vmail/mysql.aliases.cf
rm -f /tmp/vmailconfig/mysql.aliases.cf*

################################################################################

echo "Updating /etc/postfix/main.cf...";

cp /etc/postfix/main.cf /tmp/vmailconfig/postfix.main.cf.1
chmod +rrr /tmp/vmailconfig/postfix.main.cf.1
sed 's/^alias_database = hash:\/etc\/aliases/#alias_database = hash:\/etc\/aliases/g' < /tmp/vmailconfig/postfix.main.cf.1 > /tmp/vmailconfig/postfix.main.cf.2
sed 's/^myorigin = \/etc\/mailname/#myorigin = \/etc\/mailname/g' < /tmp/vmailconfig/postfix.main.cf.2 > /tmp/vmailconfig/postfix.main.cf.3
sed 's/^mailbox_command = .*/#mailbox_command = procmail -a "$EXTENSION"/g' < /tmp/vmailconfig/postfix.main.cf.3 > /tmp/vmailconfig/postfix.main.cf.4
sed 's/^myhostname = .*/myhostname = '${INPUT_HOSTNAME}'/g' < /tmp/vmailconfig/postfix.main.cf.4 > /tmp/vmailconfig/postfix.main.cf.5
sed 's/^mydestination = .*/mydestination = $myhostname localhost/g' < /tmp/vmailconfig/postfix.main.cf.5 > /tmp/vmailconfig/postfix.main.cf.6

if [ `grep "Added by VMail installer" /etc/postfix/main.cf | wc -l` = 0 ]
then
	sed 's|__MAIL_DIR__|'${INPUT_MAIL_DIR}'|g' < /tmp/postfix.main.cf.additions >> /tmp/vmailconfig/postfix.main.cf.6
fi
rm -f /tmp/postfix.main.cf.additions

cp --no-preserve=mode,ownership /tmp/vmailconfig/postfix.main.cf.6 /etc/postfix/main.cf
rm -f /tmp/vmailconfig/postfix.main.cf*

################################################################################

if [ `grep "Added by VMail installer" /etc/postfix/master.cf | wc -l` = 0 ]
then
	echo "Updating /etc/postfix/master.cf...";
	cp /etc/postfix/master.cf /tmp/vmailconfig/postfix.master.cf.1
	chmod +rrr /tmp/vmailconfig/postfix.master.cf.1
	cat /tmp/postfix.master.cf.additions >> /tmp/vmailconfig/postfix.master.cf.1

	cp --no-preserve=mode,ownership /tmp/vmailconfig/postfix.master.cf.1 /etc/postfix/master.cf
	rm /tmp/vmailconfig/postfix.master.cf*
fi
rm -f /tmp/postfix.master.cf.additions

################################################################################

echo "Updating /etc/clamav/clamd.conf...";

cp /etc/clamav/clamd.conf /tmp/vmailconfig/clamd.conf.1
chmod +rrr /tmp/vmailconfig/clamd.conf.1
sed 's/^LocalSocket.*/#LocalSocket \/var\/run\/clamav\/clamd.ctl/g' < /tmp/vmailconfig/clamd.conf.1 > /tmp/vmailconfig/clamd.conf.2
sed 's/^FixStaleSocket.*/#FixStaleSocket true\nTCPSocket 3310\nTCPAddr 127.0.0.1/g' < /tmp/vmailconfig/clamd.conf.2 > /tmp/vmailconfig/clamd.conf.3
cp --no-preserve=mode,ownership /tmp/vmailconfig/clamd.conf.3 /etc/clamav/clamd.conf
rm -f /tmp/vmailconfig/clamd.conf*

################################################################################

echo "Updating /etc/dspam/dspam.d/mysql.conf...";

cp /etc/dspam/dspam.d/mysql.conf /tmp/vmailconfig/dspam.mysql.conf.1
chmod +rrr /tmp/vmailconfig/dspam.mysql.conf.1
sed 's/^MySQLUser.*/MySQLUser	dspam/g' < /tmp/vmailconfig/dspam.mysql.conf.1 > /tmp/vmailconfig/dspam.mysql.conf.2
sed 's/^MySQLPass.*/MySQLPass	'$INPUT_PASSWORD'/g' < /tmp/vmailconfig/dspam.mysql.conf.2 > /tmp/vmailconfig/dspam.mysql.conf.3
sed 's/^MySQLDb.*/MySQLDb	'$INPUT_DATABASE'/g' < /tmp/vmailconfig/dspam.mysql.conf.3 > /tmp/vmailconfig/dspam.mysql.conf.4
sed 's/#MySQLVirtualTable.*/MySQLVirtualTable	mailusers/g' < /tmp/vmailconfig/dspam.mysql.conf.4 > /tmp/vmailconfig/dspam.mysql.conf.5
sed 's/#MySQLVirtualUIDField.*/MySQLVirtualUIDField	id/g' < /tmp/vmailconfig/dspam.mysql.conf.5 > /tmp/vmailconfig/dspam.mysql.conf.6
sed 's/#MySQLVirtualUsernameField.*/MySQLVirtualUsernameField	email/g' < /tmp/vmailconfig/dspam.mysql.conf.6 > /tmp/vmailconfig/dspam.mysql.conf.7
sed 's/#MySQLUIDInSignature.*/MySQLUIDInSignature	on/g' < /tmp/vmailconfig/dspam.mysql.conf.7 > /tmp/vmailconfig/dspam.mysql.conf.8
cp --no-preserve=mode,ownership /tmp/vmailconfig/dspam.mysql.conf.8 /etc/dspam/dspam.d/mysql.conf
rm -f /tmp/vmailconfig/dspam.mysql.conf*

################################################################################

echo "Updating /etc/dspam/dspam.conf";
cp /etc/dspam/dspam.conf /tmp/vmailconfig/dspam.conf.1
chmod +rrr /tmp/vmailconfig/dspam.conf.1

sed 's/StorageDriver \/usr\/lib\/dspam\/libhash_drv.so/StorageDriver \/usr\/lib\/dspam\/libmysql_drv.so/g' < /tmp/vmailconfig/dspam.conf.1 > /tmp/vmailconfig/dspam.conf.2
sed 's/^#ClamAVPort.*/ClamAVPort	3310/g' < /tmp/vmailconfig/dspam.conf.2 > /tmp/vmailconfig/dspam.conf.3
sed 's/^#ClamAVHost.*/ClamAVHost	127.0.0.1/g' < /tmp/vmailconfig/dspam.conf.3 > /tmp/vmailconfig/dspam.conf.4
sed 's/^#ClamAVResponse.*/ClamAVResponse	reject/g' < /tmp/vmailconfig/dspam.conf.4 > /tmp/vmailconfig/dspam.conf.5
sed 's/^#DeliveryHost.*/DeliveryHost	127.0.0.1/g' < /tmp/vmailconfig/dspam.conf.5 > /tmp/vmailconfig/dspam.conf.6
sed 's/^#DeliveryPort.*/DeliveryPort	10025/g' < /tmp/vmailconfig/dspam.conf.6 > /tmp/vmailconfig/dspam.conf.7
sed 's/^#DeliveryIdent.*/DeliveryIdent	localhost/g' < /tmp/vmailconfig/dspam.conf.7 > /tmp/vmailconfig/dspam.conf.8
sed 's/^#DeliveryProto.*/DeliveryProto	SMTP/g' < /tmp/vmailconfig/dspam.conf.8 > /tmp/vmailconfig/dspam.conf.9
sed 's/^Trust mailnull/Trust vmail/g' < /tmp/vmailconfig/dspam.conf.9 > /tmp/vmailconfig/dspam.conf.10
sed 's/^#Feature tb=.*/Feature tb=5/g' < /tmp/vmailconfig/dspam.conf.10 > /tmp/vmailconfig/dspam.conf.11
sed 's/^#ImprobabilityDrive.*/ImprobabilityDrive on/g' < /tmp/vmailconfig/dspam.conf.11 > /tmp/vmailconfig/dspam.conf.12
sed 's/AllowOverride whitelistThreshold/AllowOverride whitelistThreshold\nAllowOverride localStore/g' < /tmp/vmailconfig/dspam.conf.12 > /tmp/vmailconfig/dspam.conf.13
sed 's/Notifications.*/Notifications	on/g' < /tmp/vmailconfig/dspam.conf.13 > /tmp/vmailconfig/dspam.conf.14
sed 's/Opt in/Opt out/g' < /tmp/vmailconfig/dspam.conf.14 > /tmp/vmailconfig/dspam.conf.15
sed 's/^#TrackSources.*/TrackSources spam nonspam/g' < /tmp/vmailconfig/dspam.conf.15 > /tmp/vmailconfig/dspam.conf.16
sed 's/^#ParseToHeaders.*/ParseToHeaders on/g' < /tmp/vmailconfig/dspam.conf.16 > /tmp/vmailconfig/dspam.conf.17
sed 's/^#ChangeModeOnParse.*/ChangeModeOnParse on/g' < /tmp/vmailconfig/dspam.conf.17 > /tmp/vmailconfig/dspam.conf.18
sed 's/^#ChangeUserOnParse.*/ChangeUserOnParse full/g' < /tmp/vmailconfig/dspam.conf.18 > /tmp/vmailconfig/dspam.conf.19
sed 's/^#ServerMode.*/ServerMode	auto/g' < /tmp/vmailconfig/dspam.conf.19 > /tmp/vmailconfig/dspam.conf.20
sed 's/^#ServerParameters.*/ServerParameters	"--deliver=innocent"/g' < /tmp/vmailconfig/dspam.conf.20 > /tmp/vmailconfig/dspam.conf.21
sed 's/^#ServerIdent.*/ServerIdent	"'${INPUT_HOSTNAME}'"/g' < /tmp/vmailconfig/dspam.conf.21 > /tmp/vmailconfig/dspam.conf.22
sed 's/^#ServerDomainSocketPath.*/ServerDomainSocketPath	"\/var\/spool\/postfix\/tmp\/dspam.sock"/g' < /tmp/vmailconfig/dspam.conf.22 > /tmp/vmailconfig/dspam.conf.23
sed 's/^#ServerPID.*/ServerPID	\/var\/run\/dspam\/dspam.pid/g' < /tmp/vmailconfig/dspam.conf.23 > /tmp/vmailconfig/dspam.conf.24
sed 's/^#EnablePlusedDetail.*/EnablePlusedDetail	on/g' < /tmp/vmailconfig/dspam.conf.24 > /tmp/vmailconfig/dspam.conf.25

cp --no-preserve=mode,ownership /tmp/vmailconfig/dspam.conf.25 /etc/dspam/dspam.conf
rm -f /tmp/vmailconfig/dspam.conf*


################################################################################

echo "Updating /etc/default/dspam..."

cp /etc/default/dspam /tmp/vmailconfig/default.dspam.1
chmod +rrr /tmp/vmailconfig/default.dspam.1
sed 's/START=no/START=yes/g' < /tmp/vmailconfig/default.dspam.1 > /tmp/vmailconfig/default.dspam.2
cp --no-preserve=mode,ownership /tmp/vmailconfig/default.dspam.2 /etc/default/dspam
rm -f /tmp/vmailconfig/default.dspam*

################################################################################

echo "Updating /etc/courier/authdaemonrc..."
cp /etc/courier/authdaemonrc /tmp/vmailconfig/courier.authdaemonrc.1
chmod +rrr /tmp/vmailconfig/courier.authdaemonrc.1
sed 's/^authmodulelist=.*/authmodulelist="authmysql"/g' < /tmp/vmailconfig/courier.authdaemonrc.1 > /tmp/vmailconfig/courier.authdaemonrc.2
cp --no-preserve=mode,ownership /tmp/vmailconfig/courier.authdaemonrc.2 /etc/courier/authdaemonrc
rm -f /tmp/vmailconfig/courier.authdaemonrc*

################################################################################

echo "Updating /etc/courier/authmysqlrc"
cp /etc/courier/authmysqlrc /tmp/vmailconfig/courier.authmysqlrc.1
chmod +rrr /tmp/vmailconfig/courier.authmysqlrc.1
sed 's/^MYSQL_SERVER.*/MYSQL_SERVER	localhost/g' < /tmp/vmailconfig/courier.authmysqlrc.1 > /tmp/vmailconfig/courier.authmysqlrc.2
sed 's/^MYSQL_USERNAME.*/MYSQL_USERNAME	courier/g' < /tmp/vmailconfig/courier.authmysqlrc.2 > /tmp/vmailconfig/courier.authmysqlrc.3
sed 's/^MYSQL_PASSWORD.*/MYSQL_PASSWORD	'$INPUT_PASSWORD'/g' < /tmp/vmailconfig/courier.authmysqlrc.3 > /tmp/vmailconfig/courier.authmysqlrc.4
sed 's/^MYSQL_DATABASE.*/MYSQL_DATABASE	'$INPUT_DATABASE'/g' < /tmp/vmailconfig/courier.authmysqlrc.4 > /tmp/vmailconfig/courier.authmysqlrc.5
sed 's/^# MYSQL_SOCKET.*\/var\/run\/mysqld\/mysqld.sock/MYSQL_SOCKET	\/var\/run\/mysqld\/mysqld.sock/g' < /tmp/vmailconfig/courier.authmysqlrc.5 > /tmp/vmailconfig/courier.authmysqlrc.6
sed 's/^MYSQL_USER_TABLE.*/MYSQL_USER_TABLE	mailusers/g' < /tmp/vmailconfig/courier.authmysqlrc.6 > /tmp/vmailconfig/courier.authmysqlrc.7
sed 's/^MYSQL_UID_FIELD.*/MYSQL_UID_FIELD	uid/g' < /tmp/vmailconfig/courier.authmysqlrc.7 > /tmp/vmailconfig/courier.authmysqlrc.8
sed 's/^MYSQL_GID_FIELD.*/MYSQL_GID_FIELD	gid/g' < /tmp/vmailconfig/courier.authmysqlrc.8 > /tmp/vmailconfig/courier.authmysqlrc.9
sed 's/^MYSQL_LOGIN_FIELD.*/MYSQL_LOGIN_FIELD	email/g' < /tmp/vmailconfig/courier.authmysqlrc.9 > /tmp/vmailconfig/courier.authmysqlrc.10
sed 's/^MYSQL_HOME_FIELD.*/MYSQL_HOME_FIELD	homedir/g' < /tmp/vmailconfig/courier.authmysqlrc.10 > /tmp/vmailconfig/courier.authmysqlrc.11
sed 's/^# MYSQL_MAILDIR_FIELD.*/MYSQL_MAILDIR_FIELD	maildir/g' < /tmp/vmailconfig/courier.authmysqlrc.11 > /tmp/vmailconfig/courier.authmysqlrc.12

sed 's/^MYSQL_NAME_FIELD.*/#MYSQL_NAME_FIELD	name/g' < /tmp/vmailconfig/courier.authmysqlrc.12 > /tmp/vmailconfig/courier.authmysqlrc.13
sed 's/^MYSQL_CRYPT_PWFIELD.*/#MYSQL_CRYPT_PWFIELD	crypt/g' < /tmp/vmailconfig/courier.authmysqlrc.13 > /tmp/vmailconfig/courier.authmysqlrc.14
sed 's/^# MYSQL_CLEAR_PWFIELD.*/MYSQL_CLEAR_PWFIELD	password/g' < /tmp/vmailconfig/courier.authmysqlrc.14 > /tmp/vmailconfig/courier.authmysqlrc.15

cp --no-preserve=mode,ownership /tmp/vmailconfig/courier.authmysqlrc.15 /etc/courier/authmysqlrc
rm -f /tmp/vmailconfig/courier.authmysqlrc*

################################################################################

echo "Applying patches... "
patch /etc/init.d/dspam /tmp/dspam-initd.patch
rm -f /tmp/dspam-initd.patch

################################################################################

echo ""

################################################################################

echo ""
echo "Creating VMail system user..."
adduser vmail --home $INPUT_MAIL_DIR --uid 701 --group --system --disabled-login
echo ""

################################################################################

echo "Creating VMail database...";
sed 's|__DATABASE__|'$INPUT_DATABASE'|g' < /tmp/vmail.sql > /tmp/vmailconfig/vmail.sql.1
sed 's|__PASSWORD__|'$INPUT_PASSWORD'|g' < /tmp/vmailconfig/vmail.sql.1 > /tmp/vmailconfig/vmail.sql.2
sed 's|__MAIL_DIR__|'${INPUT_MAIL_DIR}'|g' < /tmp/vmailconfig/vmail.sql.2 > /tmp/vmailconfig/vmail.sql.3
cp /tmp/vmailconfig/vmail.sql.3 /tmp/vmail.sql
rm -f /tmp/vmailconfig/vmail.sql*
echo ""

echo "When prompted for your password, enter your MySQL root password, "
echo "or just press enter if you have not yet configured one."
echo ""
echo "$ mysql -u root -p < /tmp/vmail.sql"
mysql -u root -p < /tmp/vmail.sql

################################################################################

echo ""
echo "Restarting services..."
echo ""

/etc/init.d/clamav-daemon start

/etc/init.d/dspam start

if [ -f /var/lib/postfix/*.db ]
then
    chown -R postfix:postfix /var/lib/postfix/*.db
fi

/etc/init.d/postfix start

/etc/init.d/courier-authdaemon start

if [ -f /etc/init.d/courier-pop ]
then
	/etc/init.d/courier-pop start
fi

if [ -f /etc/init.d/courier-imap ]
then
	/etc/init.d/courier-imap start
fi

if [ -f /etc/init.d/courier-pop-ssl ]
then
	/etc/init.d/courier-pop-ssl start
fi

if [ -f /etc/init.d/courier-imap-ssl ]
then
	/etc/init.d/courier-imap-ssl start
fi

if [ -f /usr/bin/dspam_retrain ]
then
	chmod +x /usr/bin/dspam_retrain
fi

if [ -f /usr/bin/dspam_mysql_purge ]
then
	chmod +x /usr/bin/dspam_mysql_purge
fi

echo ""
echo "All done!"
echo ""
rm -rf /tmp/vmailconfig

rm -rf /etc/postfix/.svn /etc/postfix/vmail/.svn /etc/postfix/sasl/.svn


# EOF
